package com.lemote.action.order;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.lemote.action.BaseAction;
import com.lemote.core.Page;
import com.lemote.core.PropertyFilter;
import com.lemote.entity.CircuitBoard;
import com.lemote.entity.Flow;
import com.lemote.entity.FlowTask;
import com.lemote.entity.OrderEntity;
import com.lemote.entity.User;
import com.lemote.exception.AppException;
import com.lemote.service.order.CircuitBoardService;
import com.lemote.service.order.FlowService;
import com.lemote.service.order.FlowTaskService;
import com.lemote.service.order.OrderService;
import com.lemote.utils.CheckUtils;
import com.lemote.utils.Constant;
import com.lemote.utils.StringUtils;

/**
 * 
 * @author slaton
 * @time 2011-7-16 上午10:47:04
 * @Version:0.0.1
 * @since smt
 */
@Component("orderAction")
public class OrderAction extends BaseAction<OrderEntity, OrderService> {

	private OrderEntity orderEntity;
	
	private FlowService flowService;
	
	private FlowTaskService flowTaskService;
	
	private CircuitBoardService circuitBoardService; 
	
	/**
	 * 开始订单提示
	 */
	private String msg;

	@Override
	public String delete() throws AppException {

		if (ids != null && ids.length() > 0) {
			entityService.delete(CheckUtils.stringParse(ids));
		}
		return SUCCESS;
	}

	@Override
	public String input() throws AppException {

		prepareModel();
		if (orderEntity.getId()==null||"0".equals(orderEntity.getStatus())) {
			return SUCCESS;
		}
		return ERROR;
	}
	
	public String order(){
		prepareModel();
		return SUCCESS;
	}

	@Override
	public String list() throws AppException {

		List<PropertyFilter> filters = PropertyFilter
				.buildFromHttpRequest(request);
		page.setOrder(Page.DESC);
		page.setOrderBy("id");
		page = entityService.search(page, filters);
		return SUCCESS;
	}
	
	public String start(){
		String id = request.getParameter("order");
		if (id!=null&&!"".equals(org.apache.commons.lang.StringUtils.trim(id))) {
			Long oid = Long.parseLong(id);
			OrderEntity order = entityService.get(oid);
			order.setStatus("1");
			order.setStartDate(new Date());
			FlowTask flowTask = flowTaskService.getTaskByTaskName("repair");
			List<CircuitBoard> boards = order.getBoards();
			for (CircuitBoard c : boards) {
				c.setFlowTask(flowTask);
			}
			entityService.update(order);
		}
		this.msg = "Y";
		return SUCCESS;
	}

	@Override
	public void prepareModel(){
		if (id != null) {
			orderEntity = entityService.get(id);
		} else {
			orderEntity = new OrderEntity();
		}
	}

	@Override
	public String save() throws AppException {

		if (orderEntity != null) {
			if (orderEntity.getId() == null) {
				Flow flow = flowService.get(1L);
				orderEntity.setFlow(flow);
				entityService.add(saveOrder(orderEntity));
			} else {
				entityService.update(saveOrder(orderEntity));
			}
			return GLOBALSUCCESS;
		}
		return ERROR;
	}

	private OrderEntity saveOrder(OrderEntity orderEntity) {
		User user = (User) request.getSession().getAttribute(Constant.SESSION_USER);
		orderEntity.setStatus("0");
		orderEntity.setYield(100.0f);
		orderEntity.setCreateDate(new Date());
		List<CircuitBoard> boards = createCircuitBoard(orderEntity);
		int total = boards.size();
		orderEntity.setNum(total);
		int num = total/orderEntity.getPacksize();
		if (total%orderEntity.getPacksize()>0) {
			orderEntity.setPackNum(num+1);
		}else {
			orderEntity.setPackNum(num);
		}
		orderEntity.setOrderUser(user);
		orderEntity.setBoards(boards);
		return orderEntity;
	}

	private List<CircuitBoard> createCircuitBoard(OrderEntity orderEntity) {
		List<CircuitBoard> boards = new ArrayList<CircuitBoard>();
		Flow flow = orderEntity.getFlow();
		FlowTask flowTask = null; 
		List<FlowTask> fts = flow.getTasks();
		if (fts!=null) {
			for (FlowTask ft : fts) {
				if ("order_start".equals(ft.getTaskEname())) {
					flowTask = ft;
				}
			}
		}
		String endNo = orderEntity.getIcEndNo();
		String startNo = orderEntity.getIcStartNo();
		String sameStr = StringUtils.getSameString(startNo,endNo);
		String endStr = endNo.substring(sameStr.length(),endNo.length());
		String startStr = startNo.substring(sameStr.length(),startNo.length());
		if (endStr==null||startStr==null) {
			return null;
		}
		Integer eno = Integer.parseInt(endStr);
		Integer sno = Integer.parseInt(startStr);
		while (eno>sno) {
			StringBuffer sb = new StringBuffer(sameStr);
			CircuitBoard board = new CircuitBoard();
			board.setFlowTask(flowTask);
			board.setIcNo(sb.append(sno).toString());
			board.setOrderEntity(orderEntity);
			board.setStatus("0");
			board.setOrderNo(orderEntity.getOrderNo());
			board.setSystemName(orderEntity.getSystemName());
			board.setSystemVersion(orderEntity.getSystemVersion());
			board.setPmonVersion(orderEntity.getPmonVersion());
			board.setProgramVersion(orderEntity.getProgramVersion());
			boards.add(board);
			sno++;
		}
		return boards;
	}
	
	
	@Autowired
	@Override
	public void setEntityService(OrderService orderService) {

		this.entityService = orderService;
	}

	@Override
	public OrderEntity getModel() {

		return orderEntity;
	}

	public OrderEntity getOrderEntity() {
		return orderEntity;
	}

	public void setOrderEntity(OrderEntity orderEntity) {
		this.orderEntity = orderEntity;
	}
	@Autowired
	public void setFlowService(FlowService flowService) {
		this.flowService = flowService;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	@Autowired
	public void setCircuitBoardService(CircuitBoardService circuitBoardService) {
		this.circuitBoardService = circuitBoardService;
	}

	@Autowired
	public void setFlowTaskService(FlowTaskService flowTaskService) {
		this.flowTaskService = flowTaskService;
	}
	
}
